// lisclient.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "lisclient.h"
#include "WindowSetup.h"
#include "Engine.h"
#include "stdlis.h"
#include "StdLog.h"
#include <tinyxml.h>
#include "XmlConfig.h"
#include "TimerController.h"
#include "Messenger.h"
#include "StdConsole.h"
#include "MCEConsole.h"

extern "C"
{
	#include <lua.h>
	#include <lualib.h>
	#include <lauxlib.h>
}

/*
void tests()
{
	XmlConfig config;
	config.Open("config/startup.xml");
	char channelselection[100];
	bool suc = config.TransferString("logging/channelselection", channelselection, 100, XmlConfig::Read);

	lua_State *L = lua_open();
	lua_close(L);
}
*/

void CreateInstances(void)
{
	// Start Standard Log and welcome everyone. We need to call LogSystem::DestroyInstance() to
	// delete all LogViews only.
	LogSystem::CreateInstance();
	MCEConsole::CreateInstance();
	DeviceManager::CreateInstance();
	Messenger::CreateInstance();
	TimerController::CreateInstance();
	Messenger::GetInstance()->Start();
	TimerController::GetInstance()->Start();
}

void DestroyInstances(void)
{
	DeviceManager::DestroyInstance();
	TimerController::GetInstance()->Stop();
	TimerController::GetInstance()->Join();
	TimerController::DestroyInstance();
	Messenger::GetInstance()->Stop();
	Messenger::GetInstance()->Join();
	Messenger::DestroyInstance();
	LogSystem::DestroyInstance();
	if (StdConsole::GetInstance())
		StdConsole::DestroyInstance();
	MCEConsole::DestroyInstance();
}

HRESULT Run(void)
{

	//tests();

	CreateInstances();

	{
		Engine eng;
		eng.Init();
		eng.Loop();
		eng.Uninit();
	}

	DestroyInstances();

	return S_OK;
}

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
	//_CrtSetBreakAlloc(94);
	Run();
	ASSERT(_CrtDumpMemoryLeaks() == 0);
	return (int)0;
}

